/*
 * $Id: user32wrapper.c,v 1.1 2006/05/20 07:20:36 jdfekte Exp $
 *
 * Copyright (c) 2001-2002 Pierre Dragicevic, Jean-Daniel Fekete and
 * Ecole des Mines de Nantes.  All rights reserved.
 *
 * This software is proprietary information of Pierre Dragicevic, 
 * Jean-Daniel Fekete Ecole des Mines de Nantes.  You shall use it
 * only in accordance with the terms of the license agreement you
 * accepted when downloading this software.  The license is available
 * in the file licence.txt and at the following URL:
 * http://www.emn.fr/fekete/ICon/licence.html
 */

#include <windows.h> // windows definitions
#include <Winuser.h> // User 32
#include <jni.h>
#include "fr_emn_win32input_juser32_JUser32.h"

RECT r;

/*
 * Class:     fr_emn_win32input_juser32_JUser32
 * Method:    FindWindow
 */
JNIEXPORT jint JNICALL Java_fr_emn_win32input_juser32_JUser32_FindWindow
  (JNIEnv *env, jclass class, jstring title) {
  jint hwnd;
  const char *t = (*env)->GetStringUTFChars(env, title, 0);
  
  hwnd = (jint)FindWindow(NULL, t);

  (*env)->ReleaseStringUTFChars(env, title, t);

  return hwnd;
}


/*
 * Class:     fr_emn_win32input_juser32_JUser32
 * Method:    SetCursorPos
 * Signature: (II)I
 */
JNIEXPORT jboolean JNICALL Java_fr_emn_win32input_juser32_JUser32_SetCursorPos
  (JNIEnv *env, jclass class, jint x, jint y) {
  
  return SetCursorPos(x, y);
}


/*
 * Class:     fr_emn_win32input_juser32_JUser32
 * Method:    GetMouse
 * Signature: ()[I
 */
JNIEXPORT jintArray JNICALL Java_fr_emn_win32input_juser32_JUser32_GetMouse
  (JNIEnv *env, jclass class) {
  int values[] = {-1, -1, -1};
  jintArray ret; 
 
  // RETURNS ALWAYS ZERO !
  if (SystemParametersInfo(SPI_GETMOUSE, 0, values, 0)) {
    //printf("%d %d %d\n", values[0], values[1], values[2]);
    ret = (*env)->NewIntArray(env, 3);
    (*env)->SetIntArrayRegion(env, ret, 0, 3, values);
    return ret;
  }
  else
    return NULL;
}


/*
 * Class:     fr_emn_win32input_juser32_JUser32
 * Method:    GetDoubleClickTime
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_fr_emn_win32input_juser32_JUser32_GetDoubleClickTime
  (JNIEnv *env, jclass class) {
  return GetDoubleClickTime();
}


/*
 * Class:     fr_emn_win32input_juser32_JUser32
 * Method:    ClipCursor
 * Signature: (IIII)V
 */
JNIEXPORT jboolean JNICALL Java_fr_emn_win32input_juser32_JUser32_ClipCursor
  (JNIEnv *env, jclass class, jint x, jint y, jint w, jint h) {
  	
  	r.left = x;
  	r.top = y;
  	r.right = x+w-1;
  	r.bottom = y+h-1;
  	
  	return ClipCursor(&r);
}

/*
 * Class:     fr_emn_win32input_juser32_JUser32
 * Method:    UnclipCursor
 * Signature: ()V
 */
JNIEXPORT jboolean JNICALL Java_fr_emn_win32input_juser32_JUser32_UnclipCursor
  (JNIEnv *env, jclass class) {
  return ClipCursor(NULL);
}
  
JNIEXPORT jintArray JNICALL Java_fr_emn_win32input_juser32_JUser32_GetClipCursor
  (JNIEnv *env, jclass class) {
  int values[] = {-1, -1, -1, -1};
  jintArray ret; 
 
  RECT r2;
  GetClipCursor(&r2);
  values[0] = r2.left;
  values[1] = r2.top;
  values[2] = r2.right;
  values[3] = r2.bottom;
 
  ret = (*env)->NewIntArray(env, 4);
  (*env)->SetIntArrayRegion(env, ret, 0, 4, values);
  return ret;
}